package com.webank.weideagroup2.web.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.webank.weideagroup2.bean.*;
import com.webank.weideagroup2.kit.CommonRecordKit;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import com.webank.weideagroup2.service.RecordService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/api/record")
public class RecordController {
    @Autowired
    private RecordService recordService;


    /*
     * 上传购买记录
     * return ResultBean
     */
    @PostMapping("/purchaseRecord")
    public ResultBean uploadPurchaseRecode(HttpServletRequest httpServletRequest, @RequestBody String purchaseStr) throws Exception {

        ResultBean resultBean = new ResultBean();
        PurchaseRecord purchaseRecord = new PurchaseRecord();
        HttpSession session = httpServletRequest.getSession();
        Platform platform = (Platform) session.getAttribute("_platform");
        int resultcode = -1;
        try {
            JSONObject jsonObject = JSON.parseObject(purchaseStr);
            //配置CommonRecord信息
            purchaseRecord = (PurchaseRecord) CommonRecordKit.setRecord(jsonObject, purchaseRecord, platform.getPlatform());

            String carId = jsonObject.getString("carId");
            Double price = Double.valueOf(jsonObject.getString("price"));
            String city = jsonObject.getString("location");
            String purchaseDate = jsonObject.getString("purchaseDate");
            purchaseRecord.setVIN(carId);
            purchaseRecord.setCity(city);
            purchaseRecord.setPrice(price);
            purchaseRecord.setPurchaseDate(purchaseDate );
            if (!CommonRecordKit.judgeRecord(purchaseRecord)) {
                resultBean.setStatus(ResultCode.ILLEGAL_NULL);
                resultBean.setMsg("相关必填信息未填或carId出错，请检查");
                return resultBean;
            }

        } catch (JSONException e) {

            resultBean.setStatus(ResultCode.ILLEGAL_NULL);
            resultBean.setMsg("相关必填信息未填，请检查");
            return resultBean;
        }

        resultcode = recordService.uploadPurchaseRecord(purchaseRecord);
        if (resultcode == ResultCode.SUCCESS) {
            resultBean.setMsg("上传成功");
        } else {
            resultBean.setMsg("车辆购买记录添加错误");
        }
        resultBean.setStatus(resultcode);
        return resultBean;
    }


    /*
     * 增加购买保险记录
     */

    @PostMapping("/insuranceRecord")
    public ResultBean insuranceRecord(HttpServletRequest httpServletRequest, @RequestBody String insuranceRecordStr) throws Exception {


        InsuranceRecord insuranceRecord = new InsuranceRecord();
        ResultBean resultBean = new ResultBean();
        HttpSession session = httpServletRequest.getSession();
        Platform platform = (Platform) session.getAttribute("_platform");
        try{
            JSONObject jsonObject = JSON.parseObject(insuranceRecordStr);
            //配置CommonRecord信息
            insuranceRecord = (InsuranceRecord) CommonRecordKit.setRecord(jsonObject, insuranceRecord, platform.getPlatform());
            String carId = jsonObject.getString("carId");
            String type = jsonObject.getString("type");
            String purchaseDate = jsonObject.getString("purchaseDate");
            insuranceRecord.setVIN(carId);
            insuranceRecord.setType(type);
            insuranceRecord.setPurchaseDate(purchaseDate);
            if (!CommonRecordKit.judgeRecord(insuranceRecord)) {
                resultBean.setStatus(ResultCode.ILLEGAL_NULL);
                resultBean.setMsg("相关必填信息未填或carId出错，请检查");
                return resultBean;
            }


        }catch (JSONException e){
            resultBean.setStatus(ResultCode.ILLEGAL_NULL);
            resultBean.setMsg("相关必填信息未填，请检查");
            return resultBean;
        }

        int resultCode = recordService.addInsuranceRecord(insuranceRecord);

        if (resultCode == ResultCode.SUCCESS) {
            resultBean.setMsg("添加信息成功");
        } else {
            resultBean.setMsg("车辆购买保险记录添加错误");
        }
        resultBean.setStatus(resultCode);

        return resultBean;
    }


    /**
     * 上传出险记录
     *
     * @return
     * @throws JSONException
     */
    @PostMapping(value = "/accidentRecord")
    public ResultBean addAccidentRecord(HttpServletRequest httpServletRequest, @RequestBody String string) {

        AccidentRecord ar = new AccidentRecord();
        ResultBean result = new ResultBean();
        HttpSession session = httpServletRequest.getSession();
        Platform platform = (Platform) session.getAttribute("_platform");    
        
        try{
            JSONObject record = JSON.parseObject(string);
        	 
        	 //配置CommonRecord信息
        	 ar = (AccidentRecord) CommonRecordKit.setRecord(record, ar, platform.getPlatform());
        	 ar.setVIN(record.getString("carId"));
             if (!CommonRecordKit.judgeRecord(ar)) {
                 result.setStatus(ResultCode.ILLEGAL_NULL);
                 result.setMsg("输入有误");
             }else{
            	 ar.setPart(record.getString("part"));
                 ar.setLevel(record.getString("level"));

                 int resultCode = recordService.addAccidentRecordService(ar);

                 if (resultCode == ResultCode.SUCCESS) {
                     result.setMsg("添加信息成功");
                 } else {
                	 result.setMsg("车辆购买保险记录添加错误");
                 }
                 result.setStatus(resultCode);            	 
             }        	
        }catch(Exception e){
        	 result.setStatus(ResultCode.ILLEGAL_NULL);
        	 result.setMsg("相关必填信息未填，请检查");       
        }
        return result;
    }


    /**
     * 上传年检记录
     *
     * @return
     * @throws JSONException
     */
    @PostMapping(value = "/inspectionRecord")

    public ResultBean addInspectionRecord(HttpServletRequest httpServletRequest, @RequestBody String string) throws Exception {
        
    	InspectionRecord ir = new InspectionRecord();
        ResultBean result = new ResultBean();
        HttpSession session = httpServletRequest.getSession();
        Platform platform = (Platform) session.getAttribute("_platform");    
        
        try{
            JSONObject record = JSON.parseObject(string);
        	 //配置CommonRecord信息
        	 ir = (InspectionRecord) CommonRecordKit.setRecord(record, ir, platform.getPlatform());
        	 ir.setVIN(record.getString("carId"));
             if (!CommonRecordKit.judgeRecord(ir)) {
                 result.setStatus(ResultCode.ILLEGAL_NULL);
                 result.setMsg("输入有误");
             }else{
            	 ir.setRegistrationDate(record.getString("registrationDate"));
            	 ir.setStolenVehicle(record.getBoolean("stolenVehicle"));
            	 ir.setMortgageDate(record.getString("mortgageDate"));
            	 ir.setModifiedVehicle(record.getBoolean("modifiedVehicle"));
            	 ir.setLogoutDate(record.getString("logoutDate"));

                 int resultCode = recordService.addInspectionRecordService(ir);

                 if (resultCode == ResultCode.SUCCESS) {
                     result.setMsg("添加信息成功");
                 } else {
                	 result.setMsg("车辆购买保险记录添加错误");
                 }
                 result.setStatus(resultCode);            	 
             }        	
        }catch(Exception e){
        	 result.setStatus(ResultCode.ILLEGAL_NULL);
        	 result.setMsg("相关必填信息未填，请检查");       
        }
        return result;

    }

    /**
     * 上传二手车交易记录
     *
     * @return
     * @throws JSONException
     */
    @PostMapping(value = "/transactionRecord")

    public ResultBean addTransactionRecord(HttpServletRequest httpServletRequest, @RequestBody String string) throws Exception {

    	TransactionRecord tr = new TransactionRecord();
        ResultBean result = new ResultBean();
        HttpSession session = httpServletRequest.getSession();
        Platform platform = (Platform) session.getAttribute("_platform");    
        
        try{
            JSONObject record = JSON.parseObject(string);
        	 //配置CommonRecord信息
        	 tr = (TransactionRecord) CommonRecordKit.setRecord(record, tr, platform.getPlatform());
        	 tr.setVIN(record.getString("carId"));
             if (!CommonRecordKit.judgeRecord(tr)) {
                 result.setStatus(ResultCode.ILLEGAL_NULL);
                 result.setMsg("输入有误");
             }else{
            	 tr.setCity(record.getString("city"));

                 int resultCode = recordService.addTransactionRecordService(tr);

                 if (resultCode == ResultCode.SUCCESS) {
                     result.setMsg("添加信息成功");
                 } else {
                	 result.setMsg("车辆购买保险记录添加错误");
                 }
                 result.setStatus(resultCode);            	 
             }        	
        }catch(JSONException e){
        	 result.setStatus(ResultCode.ILLEGAL_NULL);
        	 result.setMsg("相关必填信息未填，请检查");       
        }
        return result;
    }

}
